﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ClubChengyuanModel : MonoBehaviour
{
    private static ClubChengyuanModel instance;

    public UILabel _name;
    public UILabel _id;
    public UILabel _clubNum;
    public Transform Grid;
    public GameObject Chird;
    public GameObject CloseBtn;
    public GameObject Quit;
    public GameObject Share;
    public GameObject moreBtn;
    private int _page = 1;

    public static ClubChengyuanModel Instance
    {
        get
        {
            return instance;
        }

    }

    void Awake()
    {
        instance = this;
        Chird.SetActive(false);
        UIEventListener.Get(CloseBtn).onClick = ButtonClick;
        UIEventListener.Get(moreBtn).onClick = ButtonClick;
    }
    private void ButtonClick(GameObject go)
    {
        if (go.name == "CloseBtn")
        {
            Destroy(gameObject);
        }
        else if (go.name == "More")
        {
            GetMembersRequest();
        }
    }
    public void Init(NnClubMembersResponse resp)
    {
        _id.text = HallModel.Instance._clubid.ToString();
        _clubNum.text = string.Format("{0} / 200", HallModel.Instance._clubNum.ToString());

        for (int i = 0; i < resp.Members.Count; i++)
        {
            GameObject go = GameObject.Instantiate(Chird);
            go.name = resp.Members[i].Id.ToString();
            go.transform.Find("id").GetComponent<UILabel>().text = resp.Members[i].Id.ToString();
            go.transform.Find("name").GetComponent<UILabel>().text = resp.Members[i].Nickname;
            if (!resp.Members[i].Online)
            {
                go.transform.Find("isOnline/offline").gameObject.SetActive(true);
                go.transform.Find("isOnline/online").gameObject.SetActive(false);
            }
            else
            {
                go.transform.Find("isOnline/offline").gameObject.SetActive(false);
                go.transform.Find("isOnline/online").gameObject.SetActive(true);
            }
            if (i == 0)
            {
                go.transform.Find("ishuizhang").gameObject.SetActive(true);
            }
            else
            {
                go.transform.Find("ishuizhang").gameObject.SetActive(false);
            }
            go.transform.SetParent(Grid);
            go.transform.localScale = Vector3.one;
            go.transform.localPosition = Vector3.zero;
            go.transform.localRotation = Quaternion.identity;
           
            go.SetActive(true);
        }
        Grid.GetComponent<UIGrid>().enabled = true;
    }

    /// <summary>
    /// 请求获取俱乐部成员消息
    /// </summary>
    private void GetMembersRequest()
    {
        NnClubMembersRequest req = new NnClubMembersRequest();
        req.ClubId = HallModel.Instance._clubid;
        req.Page = _page + 1;
        SocketManager sm = SocketManager.Instance;
        sm.SendProtobuf1(req, MsgId.NnClubMembersRequest);
    }
    void OnDestroy()
    {
        instance = null;
    }
}
